package com.cyh.pangu.ds;

import java.util.Arrays;
import java.util.LinkedList;

public class MyTreeDemo {

    /**
     * 此处必须要有null,没有null无法构成
     * @param inputList
     * @return
     */
    public static TreeNode createBinaryTree(LinkedList<Integer> inputList) {
        if (inputList == null || inputList.size() == 0) {
            return null;
        }
        TreeNode treeNode = null;
        Integer data = inputList.removeFirst();
        System.out.println("data:" + data);
        if (data != null) {
            treeNode = new TreeNode(data);
            treeNode.left =  createBinaryTree(inputList);
            treeNode.right = createBinaryTree(inputList);
        }


        return treeNode;
    }

    public void preOrderTraversal(TreeNode treeNode) {
        if (treeNode == null) {
            return ;
        }
        System.out.println(treeNode.data);
        preOrderTraversal(treeNode.left);
        preOrderTraversal(treeNode.right);
    }

    public void inOrderTraversal(TreeNode treeNode) {
        if (treeNode == null) {
            return ;
        }
        inOrderTraversal(treeNode.left);
        System.out.println(treeNode.data);
        inOrderTraversal(treeNode.right);
    }

    public void postOrderTraversal(TreeNode treeNode) {
        if (treeNode == null) {
            return ;
        }
        postOrderTraversal(treeNode.left);
        postOrderTraversal(treeNode.right);
        System.out.println(treeNode.data);
    }

    private static class TreeNode {
        int data;
        TreeNode left;
        TreeNode right;

        TreeNode(int data) {
            this.data =  data;
        }
    }

    public static void main(String[] args) {
        LinkedList<Integer> inputList
                = new LinkedList<>(Arrays.asList(new Integer[]{3, 2, 9, null, null, 10, null, null, 8, null, 4}));
        MyTreeDemo myTreeDemo = new MyTreeDemo();
        TreeNode treeNode =  myTreeDemo.createBinaryTree(inputList);
//        System.out.println("先序");
//        myTreeDemo.preOrderTraversal(treeNode);
//        System.out.println("中序");
//        myTreeDemo.inOrderTraversal(treeNode);
//        System.out.println("后序");
//        myTreeDemo.postOrderTraversal(treeNode);
    }
}
